/**
 * @author Lenovo
 * @date 2022/3/7
 */
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.net.URL;
import java.util.Calendar;
import java.util.Objects;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;

//棋盘宽度650*700 偏移量 40*40 单元格象素 天元和星
//左边为BorderLayout、右边为BoxLayout
public  class GameFrame extends JFrame {
    GamePanel gamePanel = new GamePanel();
    JButton undo = new JButton("悔棋");
    JCheckBox showOrder = new JCheckBox("显示落子顺序");
    JButton newGame = new JButton("新游戏");
    JRadioButton renren = new JRadioButton("人人");
    JRadioButton renji = new JRadioButton("人机");
    JComboBox<Integer> depth = new JComboBox<Integer>(new Integer[] {1,3,5});
    JComboBox<Integer> nodeCount = new JComboBox<Integer>(new Integer[] {3,5,10});
    JRadioButton guzhi = new JRadioButton("估值函数");
    JRadioButton  guzhiAndTree= new JRadioButton("估值函数+搜索数");
    public static JTextArea area=new JTextArea(20,100);
    JButton openMusic=new JButton("开启音效");
    JButton closeMusic=new JButton("关闭音效");
    JLabel area1=new JLabel();

    public void start() {
        Splash splash = new Splash();
        long startTime = System.currentTimeMillis();
        long endTime = startTime + 1000;
        //停1s
        while(System.currentTimeMillis()<endTime);
        splash.dispose();

        this.setTitle("五子棋");
        //添加右侧功能区设置
       // this.setIconImage(Toolkit.getDefaultToolkit().createImage(GameUtil.ICON_PATH));// 给窗体添加一个图标
        URL imgUrl = Test.class.getResource(GameUtil.ICON_PATH);
        assert imgUrl != null;
        ImageIcon img1 = new ImageIcon(imgUrl);
        Image imageObj= img1.getImage();
        this.setIconImage(imageObj);// 给窗体添加一个图标

        this.add(gamePanel, BorderLayout.WEST);//黄色面板
        this.setResizable(true);//不允许缩放
        this.setSize(GameUtil.GAME_WINDTH, GameUtil.GAME_HEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        undo.addMouseListener(mouselistener);
        showOrder.addMouseListener(mouselistener);
        newGame.addMouseListener(mouselistener);
        openMusic.addMouseListener(mouselistener);
        closeMusic.addMouseListener(mouselistener);

        //添加功能
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
        //多行文本框

        JPanel panel = new JPanel();
        JPanel panel1 = new JPanel();
        //border显示标题
        area.setEditable(true);
        add(new  JScrollPane(area) );
        panel1.setBorder(new TitledBorder("右键单击显示可参考的棋子坐标"));
        Calendar c = Calendar.getInstance();
        String nowtime = c.get(YEAR) +"年"+(c.get(MONTH)+1)+" 月"+(c.get(Calendar.DAY_OF_MONTH)) +"日" +c.get(Calendar.HOUR_OF_DAY) + "时" + c.get(Calendar.MINUTE) + "分" + c.get(Calendar.SECOND)+"秒";
        area1.setText("本次进入游戏的时间是"+nowtime);
        panel1.add(area);panel.add(area1);
        rightPanel.add(panel); rightPanel.add(panel1);

        JPanel panel2 = new JPanel();
        ButtonGroup group = new ButtonGroup();
        //border显示标题
        panel2.setBorder(new TitledBorder("模式"));
        group.add(renren);
        group.add(renji);
        panel2.add(renren);
        panel2.add(renji);
        rightPanel.add(panel2);


        JPanel panel3 = new JPanel();
        ButtonGroup group1 = new ButtonGroup();
        //border显示标题
        panel3.setBorder(new TitledBorder("智能"));
        panel3.add(guzhi);
        panel3.add(guzhiAndTree);
        group1.add(guzhiAndTree);
        group1.add(guzhi);
        rightPanel.add(panel3);


        JPanel panel4 = new JPanel();
        panel4.setBorder(new TitledBorder("搜索树"));
        panel4.add(new JLabel("搜索深度"));

        panel4.add(depth);
        panel4.add(new JLabel("每层结点数"));

        panel4.add( nodeCount);
        rightPanel.add(panel4);


        JPanel panel5 = new JPanel();

        //border显示标题
        panel5.setBorder(new TitledBorder("其他"));
        panel5.add(undo);
        panel5.add(showOrder);

        rightPanel.add(panel5);
        rightPanel.add(newGame);
        rightPanel.add(openMusic);
        rightPanel.add(closeMusic);
        add(rightPanel);
        // validate();

        addMouseListener(mouselistener);
        this.setVisible(true);
    }
    private MouseListener mouselistener = new MouseAdapter() {
        //重写坐标点击函数
        public void mouseClicked( MouseEvent e ) {
            Object obj = e.getSource();
            //被点击的元素
            if (obj == undo) {
                gamePanel.undo();
                System.out.println("悔棋");
            } else if (obj == newGame) {
                System.out.println("新游戏");
                boolean  mode=renren.isSelected()?true:false;
                boolean  intel=guzhi.isSelected()?true:false;
                int depthTmp=(Integer)depth.getSelectedItem();
                int nodeCountTmp=(Integer)nodeCount.getSelectedItem();
                gamePanel.newGame( showOrder.isSelected(),mode, intel, depthTmp, nodeCountTmp);
                JOptionPane.showMessageDialog(GameFrame.this, "开始新的游戏");

            } else if (obj == showOrder) {
                //gamePanel.showOrderNumber(showOrder.isSelected());
                System.out.println("显示落子顺序");
                gamePanel.ShowOrder();
            }else if(obj==openMusic){
                gamePanel.setSoundswitch(true);
                JOptionPane.showMessageDialog(null,"音效已经开启");
            }else if(obj==closeMusic){
                gamePanel.setSoundswitch(false);
                JOptionPane.showMessageDialog(null, "音效已关闭！");
            }
        }
    };
}



